package com.gcb.controller.businesses;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.AjaxResult;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.enums.BusinessType;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.controller.BaseController;
import com.gcb.domain.dto.WharfQueryDTO;
import com.gcb.domain.dto.WharfSaveDTO;
import com.gcb.domain.vo.WharfVO;
import com.gcb.service.IWharfService;
import com.gcb.framework.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 码头管理
 */
@RestController
@RequestMapping("/wharf")
@Tag(name = "码头管理", description = "码头管理")
public class WharfController extends BaseController {

    @Resource
    private IWharfService wharfService;

    /**
     * 新增码头
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:save')")
    @PostMapping("/add")
    @Operation(summary = "新增码头")
    public AjaxResult add(@Valid @RequestBody WharfSaveDTO dto) {
        wharfService.add(dto, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 刪除
     *
     * @param id
     **/
    @PreAuthorize("@ss.hasPermi('businesses:wharf:delete')")
    @GetMapping("/delete")
    public AjaxResult delete(Long id) {
        wharfService.delete(id);
        return success();
    }

    /**
     * 编辑码头
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:save')")
    @PostMapping("/update")
    @Operation(summary = "编辑码头")
    public AjaxResult update(@Valid @RequestBody WharfSaveDTO dto) {
        if (dto.getId() == null) {
            return error("id不能为空");
        }
        wharfService.update(dto, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 码头分页列表
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:pageList')")
    @GetMapping("/pageList")
    @Operation(summary = "码头分页列表")
    public TableDataInfo pageList(WharfQueryDTO dto) {
        startPage();
        List<WharfVO> list = wharfService.list(dto);
        return getDataTable(list);
    }

    /**
     * 码头详情
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:getDetail')")
    @GetMapping("/getDetail")
    @Operation(summary = "码头详情")
    public AjaxResult detail(Long id) {
        WharfVO vo = wharfService.getDetail(id);
        return success(vo);
    }

    /**
     * 批量修改码头状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:batchUpdateStatus')")
    @GetMapping("/batchUpdateStatus")
    @Operation(summary = "批量修改码头状态")
    public AjaxResult batchUpdateState(Long[] ids, Integer status) {
        Long userId = SecurityUtils.getUserId();
        wharfService.batchUpdateStatus(Arrays.asList(ids), status, userId);
        return success();
    }

    /**
     * 批量修改码头归属商户
     *
     * @param ids
     * @param businessesId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:batchUpdateBusinesses')")
    @GetMapping("/batchUpdateBusinesses")
    @Operation(summary = "批量修改码头归属商户")
    public AjaxResult batchUpdateBusinesses(Long[] ids, Long businessesId) {
        wharfService.batchUpdateBusinesses(Arrays.asList(ids), businessesId, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 项目订单管理导出
     */
    @PreAuthorize("@ss.hasPermi('businesses:wharf:export')")
    @Log(title = "码头管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "码头管理导出")
    public void export(HttpServletResponse response, WharfQueryDTO dto) {
        List<WharfVO> list = wharfService.list(dto);
        ExcelUtil<WharfVO> util = new ExcelUtil<>(WharfVO.class);
        util.exportExcel(response, list, "码头管理导出");
    }
}
